package org.sd.task;

import org.sd.event.SolrDataDiffLargeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.util.Objects;

@Component
public class ScanSolrCoreTask {
    @Autowired
    ApplicationEventPublisher eventPublisher;
    @Autowired
    ThreadPoolTaskExecutor taskExecutor;

    /**
     * 每5秒执行一次
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void scanSolr() {
        taskExecutor.execute(() -> {
            System.out.println(">>>>>>>>>>> 扫描所有core信息开始");

            SolrDataDiffLargeEvent event = new SolrDataDiffLargeEvent(this, "测试", "1232423432");
            eventPublisher.publishEvent(event);
        });

    }


    @PreDestroy
    public void destroy() {
        if (Objects.nonNull(taskExecutor)) {
            taskExecutor.shutdown();
        }
    }
}
